package com.example.finalapplication;

import static androidx.constraintlayout.motion.utils.Oscillator.TAG;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public class FruitsActivity extends AppCompatActivity implements Runnable {

  ProgressBar bar;
   ListView listView;
   Handler handler;
     SimpleAdapter listItemAdapter;
    ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fruits);
        Intent intent5 = getIntent();

     /* listView = findViewById(R.id.mylist3);
        bar = findViewById(R.id.progressBar3);
        listView.setEmptyView(bar);


        handler = new Handler() {
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what == 6) {
                    List<String> list = (List<String>) msg.obj;
                    ListAdapter adapter = new ArrayAdapter<String>(FruitsActivity.this, android.R.layout.simple_list_item_1, list);
                    listView.setAdapter(adapter);
                }
                super.handleMessage(msg);
            }

        };*/
        listView = findViewById(R.id.mylist3);
       bar = findViewById(R.id.progressBar3);
        listView.setEmptyView(bar);
            ArrayList<HashMap<String, String>> listItems = new ArrayList<HashMap<String, String>>();
        listItemAdapter = new SimpleAdapter(this,
                listItems,
                R.layout.item_two,
                new String[]{"Itemfruitstitle"},
                new int[]{R.id.fruitstitle}
        );

       listView.setAdapter(listItemAdapter);
        //listView.setOnItemClickListener(this);
        //listView.setOnItemLongClickListener(this);
        handler = new Handler(Looper.myLooper()) {

            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what == 6) {
                    list = (ArrayList<HashMap<String, String>>) msg.obj;
                    listItemAdapter = new SimpleAdapter(FruitsActivity.this,
                            list,
                            R.layout.item_two,
                            new String[]{"fruitstitle"},
                            new int[]{R.id.fruitstitle}
                    );
                   MineAdapterTwo MineAdapterTwo = new MineAdapterTwo(FruitsActivity.this,
                            R.layout.item_two,
                            list);

                    listView.setAdapter(listItemAdapter);
                }
                super.handleMessage(msg);
            }
        };

        //SecondTask SecondTask = new SecondTask();
       // SecondTask.setHandler(handler);

        Thread t = new Thread(this);
        t.start();
    }
     public void run() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ArrayList<HashMap<String, String>> retList = new ArrayList<HashMap<String, String>>();
        try {


            Document doc = Jsoup.connect("http://www.xinsinong.com/price/list-2212.html").get();
            String title = doc.title();
            Log.i(TAG, "run:title=" + title);
            Elements uls = doc.getElementsByTag("ul");
            for (int i=2;i<uls.size();i++) {

                Element ul = uls.get(i);
                Elements lis = ul.getElementsByTag("li");
                for (Element li : lis) {
                    Elements as = li.getElementsByTag("a");
                    Log.i(TAG, "run:as.size=" + as.size());
                    if (as.size() > 0) {
                        String fruitstitle = as.get(0).text();

                        Log.i(TAG, "run:" + fruitstitle);
                         HashMap<String, String> map = new HashMap<String, String>();
                        map.put("fruitstitle", fruitstitle);
                        retList.add(map);
                    }
                }
            }


        } catch (IOException e) {
            e.printStackTrace();
        }

        Message msg = handler.obtainMessage(6, retList);
        //msg.obj = "Hello";

        handler.sendMessage(msg);
        Log.i(TAG, "run:sendMessage ok");
    }
    }
